﻿/* ######################################## *\
 * ### Copyright (C) 2009 AJ Ravindiran ### *
 * ### HTTP://THEAJ.NET/ <AJ@THEAJ.NET> ### *
\* ######################################## */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AJRavindiran.Jolt.Utilities
{
    public static class MathUtilities
    {
        /// <summary>
        /// Converts a long to a string.
        /// </summary>
        public static string LongToString(long value)
        {
            if (value <= 0L || value >= 0x5b5b57f8a98a5dd1L)
                return "invalid_name";
            if (value % 37L == 0L)
                return "invalid_name";

            int i = 0;
            char[] c = new char[12];

            while (value != 0L)
            {
                long l = value;
                value /= 37L;
                c[11 - i++] = StringUtilities.ValidCharacters[((int)(l - value * 37L))];
            }
            return new String(c, 12 - i, i);
        }

        /// <summary>
        /// Converts a string to a long.
        /// </summary>
        public static long StringToLong(string value)
        {
            long l = 0L;
            for (int i = 0; i < value.Length && i < 12; i++)
            {
                char c = value[i];
                l *= 37L;
                if (c >= 'A' && c <= 'Z')
                    l += (1 + c) - 65;
                else if (c >= 'a' && c <= 'z')
                    l += (1 + c) - 97;
                else if (c >= '0' && c <= '9')
                    l += (27 + c) - 48;
            }
            for (; l % 37L == 0L && l != 0L; l /= 37L) ;
            return l;
        }
    }
}
